package com.lagou.controller;

import com.lagou.domain.Employee;
import com.lagou.domain.dept;
import com.lagou.service.EmployeeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    //注入service
    @Autowired
    private EmployeeService employeeService;
    //查询所有用户
    @RequestMapping("/findAll")
    public  String findAll(Model model){

        //实现查询所有账户
        List<Employee> list = employeeService.findAll();

        // 把封装好的list存到model中
        model.addAttribute("list",list);

        return "list";
    }
    @RequestMapping("/save")
    public String save(Employee employee){
        System.out.println("employee = " + employee);
        employeeService.save(employee);
        // 跳转到findAll方法从新查询一次数据库进行数据的遍历展示
        return "redirect:/employee/findAll";
    }
    /*
        根据id查询职员信息，完成账户回显
     */
    @RequestMapping("/ajaxRequest")
    @ResponseBody
    public List<dept> ajaxRequest(@RequestBody List<dept> deptList){
        List<dept> dept = employeeService.findDept();
        return dept;
    }
}
